<?php

defined('BASEPATH') or die('forbidden');

class storegoods_Controller Extends Controller {

    private $inc;
    private $objectTypeList;	//物品列表
    private $objectKindList;	//物品类型
    private $professionList;	//职业类型
    private $materialTypeList;	//物品类型
    
    function _init() {
        check_level(50, segment(0), segment(1, 'index'));
        $this->inc = $this->config['inc'];
        
        $this->professionList = $this->config['inc']['type_proffessionid'];
        
        $this->initList();	//初始化数据
    }

    function index() {
      
        $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
        if ($area_host == '') {
            echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
        }
        $this->view->display("mall/storegoods/index");
    }

    function flexigrid() {
        //读取flexigrid传输的数据
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
        $strResult = vpost($area_host . "action=28");
        $row = json_decode($strResult, true);
        unset($areaarray);
        $areaarray=  get_area_list_host();
        $i=1;
        foreach ($row as $key => $value) {     
            if($i>=$offset&&$i<=($rp+$offset))
            {
                $value['area_host']=$area_host;
                $id= base64_encode(http_build_query($value));
                $value['area_host']=$areaarray[$area_host];
                $value['proffessionid']=$this->inc['type_proffessionid'][$value['proffessionid']];
                $value['hotlimit']=$value['hotlimit']==1?'true':'<font color='.'grey'.'>false</font>';
                $value['isreduce']=$value['isreduce']==false?"<font color='grey'>false</font>":$value['isreduce'];
                $value['mount']=$value['mount']==-1?  translate('MALL_STOREGOODS_NOLIMIT'):$value['mount'];
                $value['globalmount']=$value['globalmount']==-1?  translate('MALL_STOREGOODS_NOLIMIT'):$value['globalmount'];
                $value['isdown']=$value['isdown']==0?translate('MALL_STOREGOODS_DERCARRIAGE'):"<font color='grey'>".translate('MALL_STOREGOODS_UNDERCARRIAGE')."</font>";

                $rows[] = array('id' => $id, 'cell' => $value);
            }
            $i++;
        }
        $total = $i;
        
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

    function add($id = 0) {
        //是否来源于表单请求
        if (is_submit()) {
            $rowdata = $_POST['row'];
            $area_host = $rowdata['area_host'];
            $rowdata['deadtime']=strtotime($rowdata['deadtime']);
            $rowdata['starttime']=strtotime($rowdata['starttime']);
            unset($rowdata['area_host']);
            unset($rowdata['material_type']);	//不需要此字段
            $str = array_to_stringparam($rowdata);
            $str = $area_host . $str . "action=26";
            $strResult = vpost($str);

            $result = 0;
            //操作失败或者连接失败
            if ($strResult != '0' || $strResult === FALSE)
            {
                $result = 1;
            }
            else
            {
                insert_log(translate('MENU_MALL_STOREGOODS'),translate('ADD'), NULL, $rowdata);
            }
            cp_tips($result == 0 ? "success" : "error");
        }
        $tables=array('type_object'=>  translate('MALL_STOREGOODS_TYPE_OBJECT'),'dynamic_type_object'=>  translate('MALL_STOREGOODS_DYNAMIC_TYPE_OBJECT'));
        $this->view->assign('tables',$this->form->select('row[tables]', $tables, '', array('id'=>'tables','onchange'=>'change_table(this)')));
        $this->view->assign('area_host',$this->form->select('row[area_host]',get_area_list_host(),'',array('id'=>'area_host')));
        $this->view->assign('proffessionid',$this->form->select('row[proffessionid]',$this->inc['type_proffessionid'],'',array('id'=>'proffessionid')));
        
        $this->view->assign('professiontype',$this->professionList);	//职业类型
        $this->view->assign('materialtype',$this->materialTypeList);	//材料类型
		$this->view->assign('objecttype',$this->objectTypeList);	//物品类型

        
        $this->view->display("mall/storegoods/add");
    }

    function edit($id = 0) {
        if (!isset($id))
            error(translate('C_INVALID_PARAM_ID', $id));
        parse_str(base64_decode($id),$row);       
        if (!is_array($row))
        {
            error(translate('C_INVALID_PARAM_ID', $id));
        }
        //是否来源于表单请求
        if (is_submit()) {
            $rowdata = $_POST['row'];
            $area_host = $rowdata['area_host'];
            $rowdata['deadtime']=strtotime($rowdata['deadtime']);
            $rowdata['starttime']=strtotime($rowdata['starttime']);
            $rowdata['id']=$row['id'];
            unset($rowdata['area_host']);
            $str = array_to_stringparam($rowdata);
            $str = $area_host . $str . "action=27";
            $strResult = vpost($str);
            
            $result = 0;
            //操作失败或者连接失败
            if ($strResult != '0' || $strResult === FALSE)
            {
                $result = 1;
            }
            else
            {
                insert_log(translate('MENU_MALL_STOREGOODS'),translate('EDIT'), $row, $rowdata);
            }
            cp_tips($result == 0 ? "success" : "error");
        }
        
        $this->view->assign('area_host',$this->form->select('row[area_host]',get_area_list_host(),$row['area_host'],array('id'=>'area_host')));
        $this->view->assign('proffessionid',$this->form->select('row[proffessionid]',$this->inc['type_proffessionid'],$row['proffessionid'],array('id'=>'proffessionid')));
        $this->view->display("mall/storegoods/edit", array('row' => $row));
    }  
    
	function del($id = 0) {
        $c = 0;
        $r = '';
        if (!isset($id))
        {
            json_output($c, translate('C_INVALID_PARAM_ID', $id));
        }
        //解码
        parse_str(base64_decode($id),$row);       
        if (!is_array($row))
        {
            error(translate('C_INVALID_PARAM_ID', $id));
        }
        //print_r($row);    
        $str =  $row['area_host']."action=53&id=".$row['id'];
        //echo "str = $str";
        $strResult = vpost($str);
        $result = 0;
        //操作失败或者连接失败
        if ($strResult != '0' || $strResult === FALSE) {
            $result = 1;
            $r = translate('C_TIPS_ERROR_TEXT');
        } else {
            insert_log(translate('MENU_MALL_STOREGOODS'),translate('DEL'), array('id'=>$id));
            $c = 1;
            $r = translate('C_TIPS_SUCCESS_TEXT');
        }
        json_output($c, $r);
    }
    
    function initList()
    {
    	$this->objectKindList = array(

			'10'=>'装备',
			//'11'=>'材料',
			'12'=>'时装',
			'13'=>'消耗品',
			'14'=>'任务物品',
			//'15'=>'称号背包',
			'16'=>'宠物背包',
			'17'=>'祝福背包',
		);
		
		$this->materialTypeList = array(
			'10'=>'装备',
			//'11'=>'材料',
			'12'=>'时装',
			'13'=>'消耗品',
			'14'=>'任务物品',
			//'15'=>'称号背包',
			'16'=>'宠物背包',
			'17'=>'祝福背包',
		);
		
		$tmpTypeList = get_type_object_detail(); //物品信息
		$equipList = get_equipment_list();	//装备信息
		
		foreach ($tmpTypeList as $id => $value)
		{
			//echo " id = $id ";
			//print_r($value);
			$this->objectTypeList[$value['kind']][$id]['id'] = $id;
			$this->objectTypeList[$value['kind']][$id]['name'] = $value['name'];
			$this->objectTypeList[$value['kind']][$id]['buygold'] = $value['buygold'];
			$this->objectTypeList[$value['kind']][$id]['objecticon'] = $value['objecticon'];
			$this->objectTypeList[$value['kind']][$id]['profession'] = -1;	//-1表示通用
			
			if(10 == $value['kind']) //装备才分职业
			{
				$this->objectTypeList[$value['kind']][$id]['profession'] = $equipList[$id]['profession'];
			}	
			
		}
		//print_r($this->objectTypeList);
    }
}